import 'package:flutter/material.dart';

/// 监控系统头部组件
///
/// 显示系统标题、警告信息、电量状态、日期时间和系统状态
class Header extends StatelessWidget {
  /// 当前是否为深色主题
  final bool isDarkTheme;

  /// 电量百分比
  final double batteryPercentage;

  /// 当前日期时间
  final String currentDateTime;

  /// 系统状态
  final String systemStatus;

  /// 告警信息
  final String alarmMessage;

  /// 告警等级
  final AlarmLevel alarmLevel;

  /// 当前工程名称
  final String currentProject;

  /// 切换工程按钮回调
  final VoidCallback? onSwitchProject;

  const Header({
    super.key,
    required this.isDarkTheme,
    required this.batteryPercentage,
    required this.currentDateTime,
    required this.systemStatus,
    required this.alarmMessage,
    required this.alarmLevel,
    this.currentProject = '默认工程',
    this.onSwitchProject,
  });

  @override
  Widget build(BuildContext context) {
    final textColor = Colors.white;

    return Container(
      width: double.infinity,
      height: 80,
      decoration: BoxDecoration(
        // 渐变色背景 - 从深蓝到浅蓝
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            const Color(0xFF1F4068),
            const Color(0xFF16213E),
          ],
        ),
      ),
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 左侧区域：标题和警告信息
          _buildLeftSection(textColor),

          // 中间区域：日期时间、电量和状态
          _buildCenterSection(textColor),

          // 右侧区域：系统状态指示条
          _buildRightSection(),
        ],
      ),
    );
  }

  /// 构建左侧区域
  Widget _buildLeftSection(Color textColor) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 标题
        Text(
          'HIMENSEN112',
          style: TextStyle(
            color: textColor,
            fontSize: 24,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(width: 16),

        // 当前工程信息
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
          decoration: BoxDecoration(
            color: const Color(0xFF4A6FA5),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Row(
            children: [
              Icon(Icons.file_copy, color: Colors.white, size: 14),
              const SizedBox(width: 6),
              Text(
                currentProject,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ),
        const SizedBox(width: 16),

        // 警告信息
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: _getAlarmColor(),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Row(
            children: [
              Icon(Icons.warning_amber_rounded, color: Colors.white, size: 16),
              const SizedBox(width: 4),
              Text(
                alarmMessage,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  /// 构建中间区域
  Widget _buildCenterSection(Color textColor) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        // 日期时间
        Text(currentDateTime, style: TextStyle(color: textColor, fontSize: 12)),
        const SizedBox(width: 16),

        // 电量显示
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: isDarkTheme
                ? const Color(0xFF34495E)
                : const Color(0xFF3498DB),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Text(
            '${batteryPercentage.toStringAsFixed(1)}%',
            style: TextStyle(
              color: Colors.white,
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        const SizedBox(width: 16),

        // 系统状态
        Text(systemStatus, style: TextStyle(color: textColor, fontSize: 12)),
      ],
    );
  }

  /// 构建右侧区域
  Widget _buildRightSection() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        // 状态指示条
        SizedBox(
          width: 200,
          height: 30,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: List.generate(10, (index) {
              // 高亮显示特定的条（根据参考图片）
              final isHighlighted = index == 6;
              return Container(
                width: 16,
                height: 30,
                decoration: BoxDecoration(
                  color: isHighlighted ? const Color(0xFFFF9800) : Colors.grey,
                  borderRadius: BorderRadius.circular(2),
                ),
              );
            }),
          ),
        ),
        const SizedBox(width: 16),

        // 系统状态指示器
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: isDarkTheme
                ? const Color(0xFF34495E)
                : const Color(0xFF19BE6B),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Row(
            children: [
              Icon(Icons.check_circle, color: Colors.white, size: 14),
              const SizedBox(width: 4),
              Text(
                'ON',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ),
        const SizedBox(width: 16),

        // 切换工程按钮
        if (onSwitchProject != null)
          ElevatedButton(
            onPressed: onSwitchProject,
            child: Row(
              children: [
                Icon(Icons.swap_horiz, size: 14),
                const SizedBox(width: 4),
                Text('切换工程'),
              ],
            ),
            style: ElevatedButton.styleFrom(
              foregroundColor: Colors.white,
              backgroundColor: const Color(0xFF4A6FA5),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4),
              ),
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
            ),
          ),
        const SizedBox(width: 16),

        // 更多信息按钮
        IconButton(
          icon: Icon(Icons.more_vert, color: Colors.white),
          onPressed: () {},
          tooltip: '更多选项',
        ),
      ],
    );
  }

  /// 根据告警等级获取对应的颜色
  Color _getAlarmColor() {
    switch (alarmLevel) {
      case AlarmLevel.critical:
        return Colors.red;
      case AlarmLevel.high:
        return const Color(0xFFFA8C16);
      case AlarmLevel.medium:
        return const Color(0xFFFAAD14);
      case AlarmLevel.low:
        return const Color(0xFF1890FF);
      default:
        return Colors.grey;
    }
  }
}

/// 告警等级枚举
enum AlarmLevel { critical, high, medium, low, info }
